package cetc54.zhtx.server;

import cetc54.zhtx.common.ConstantPool;
import cetc54.zhtx.start.ServerListenStatus;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

public class ServerInitializedHander extends ChannelInitializer<SocketChannel> {

    private ServerListenStatus serverListenStatus;

    public ServerInitializedHander(ServerListenStatus serverListenStatus){
        this.serverListenStatus = serverListenStatus;
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        //心跳处理器
        pipeline.addLast("idStateHandler",new IdleStateHandler(ConstantPool.READ_TIME,0,0, TimeUnit.SECONDS));
        //断开连接触发器
        pipeline.addLast("serverTriggerHandler",new ServerTriggerHandler());
        //编解码器
        pipeline.addLast("stringDecoder",new StringDecoder());
        pipeline.addLast("stringEncoder",new StringEncoder());
        //心跳包处理类
        pipeline.addLast("myHeartBeatHandler",new ServerHeartBeatHandler(serverListenStatus));
    }
}
